package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.SysPermission;
import com.jyy.prefabricated.service.SysPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户权限
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class SysPermissionController {

    @Autowired
    private SysPermissionService permissionService;

    /**
     * 新增权限
     * @param permission
     * @return
     */
    @PostMapping("/permissions")
    public AjaxResult insert(@Valid @RequestBody SysPermission permission){
        log.info("Rest request to insert permission:{}",permission);
        int id = permissionService.insert(permission);
        SysPermission result = permissionService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新权限
     * @param permission
     * @return
     */
    @PutMapping("/permissions")
    public AjaxResult update(@Valid @RequestBody SysPermission permission){
        log.info("Rest request to update permission:{}",permission);
        permissionService.update(permission);
        SysPermission result = permissionService.selectById(permission.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询权限详情
     * @param id
     * @return
     */
    @GetMapping("/permissions/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find permission by id:{}",id);
        SysPermission result = permissionService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除权限
     * @param id
     * @return
     */
    @DeleteMapping("/permissions/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete permission by id:{}",id);
        int row = permissionService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 获取权限列表
     * @param pid 父级id
     * @param keyword
     * @return
     */
    @GetMapping("/permissions-list")
    public AjaxResult selectAll(@RequestParam(required = false) Integer pid, @RequestParam(required = false, defaultValue = "") String  keyword){
        log.info("Rest request to find permission list");
        List<SysPermission> result = permissionService.selectByParams(pid, keyword);
        return AjaxResult.success(result);
    }

    /**
     * 分页查询权限
     * @param pid 父级id
     * @param keyword 关键字
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/permissions")
    public AjaxResult selectByParams(@RequestParam(required = false) Integer pid, @RequestParam(required = false, defaultValue = "") String  keyword, Pageable pageable){
        log.info("Rest request to find permission by params pid:{},keyword:{}", pid,keyword);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<SysPermission> permissions = permissionService.selectByParams(pid,keyword);
        PageInfo<SysPermission> result = new PageInfo<>(permissions);
        return AjaxResult.success(result);
    }

}
